home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BSYNC.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  96 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bsync.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13.  
  14. /* local headers */
  15. #include "blkio_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      bsync - synchronize a block file
  20.  
  21. SYNOPSIS
  22.      #include <blkio.h>
  23.  
  24.      int bsync(bp)
  25.      BLKFILE *bp;
  26.  
  27. DESCRIPTION
  28.      The bsync function causes the block file associated with BLKFILE
  29.      pointer bp to be synchronized with its buffers; any buffered data
  30.      which has been written to the buffers is written to the file.
  31.      The header, if it has been modified, is written out last.  The
  32.      block file remains open and the buffers retain their contents.
  33.      If bp is open read-only or is not buffered, there will be no data
  34.      to synchronize and bsync will return a value of zero immediately.
  35.  
  36.      bsync will fail if one or more of the following is true:
  37.  
  38.      [EINVAL]       bp is not a valid BLKFILE pointer.
  39.      [BENOPEN]      bp is not open.
  40.  
  41. SEE ALSO
  42.      bexit, bflush, bputb.
  43.  
  44. DIAGNOSTICS
  45.      Upon successful completion, a value of 0 is returned.  Otherwise,
  46.      a value of -1 is returned, and errno set to indicate the error.
  47.  
  48. ------------------------------------------------------------------------------*/
  49. #ifdef AC_PROTO
  50. int bsync(BLKFILE *bp)
  51. #else
  52. int bsync(bp)
  53. BLKFILE *bp;
  54. #endif
  55. {
  56.     int i = 0;    /* loop counter */
  57.  
  58.     /* validate arguments */
  59.     if (!b_valid(bp)) {
  60.         errno = EINVAL;
  61.         return -1;
  62.     }
  63.  
  64.     /* check if not open */
  65.     if (!(bp->flags & BIOOPEN)) {
  66.         errno = BENOPEN;
  67.         return -1;
  68.     }
  69.  
  70.     /* check if not open for writing */
  71.     if (!(bp->flags & BIOWRITE)) {
  72.         return 0;
  73.     }
  74.  
  75.     /* check if not buffered */
  76.     if (bp->bufcnt == 0) {
  77.         return 0;
  78.     }
  79.  
  80.     /* synchronize block in each buffer */
  81.     for (i = 1; i <= bp->bufcnt; ++i) {
  82.         if (b_put(bp, (size_t)i) == -1) {
  83.             BEPRINT;
  84.             return -1;
  85.         }
  86.     }
  87.  
  88.     /* synchronize header */
  89.     if (b_put(bp, (size_t)0) == -1) {
  90.         BEPRINT;
  91.         return -1;
  92.     }
  93.  
  94.     return 0;
  95. }
  96.